package fr.xgouchet.wedding.client.controller;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;

import fr.xgouchet.wedding.client.event.FeedbackMessageEvent;
import fr.xgouchet.wedding.client.event.FeedbackMessageEventHandler;
import fr.xgouchet.wedding.client.presenter.FeedbackMessagePresenter;
import fr.xgouchet.wedding.client.presenter.Presenter;
import fr.xgouchet.wedding.client.view.FeedbackMessageView;

/**
 * Controller used to send feedbacks to user (error or confirmation messages)
 * 
 * @author x.gouchet
 * 
 */
public class FeedbackController implements Presenter {

	/**
	 * Constructor
	 * 
	 * @param eventBus
	 *            the event bus for this App Controller
	 */
	public FeedbackController(HandlerManager eventBus) {
		mEventBus = eventBus;
		bind();
	}

	/**
	 * Binds the app controler with the Event Bus
	 */
	private void bind() {
		mEventBus.addHandler(FeedbackMessageEvent.TYPE,
				new FeedbackMessageEventHandler() {
					public void onFeedbackMessage(FeedbackMessageEvent event) {
						doFeedbackMessage(event.getMessage(), event.isError());
					}
				});
	}

	/**
	 * Asks a presenter to print a message
	 * 
	 * @param msg
	 *            the message to print
	 * @param error
	 *            is the message error or info
	 */
	private void doFeedbackMessage(String msg, boolean error) {
		Presenter presenter = new FeedbackMessagePresenter(
				new FeedbackMessageView(), msg, error);
		presenter.go(mContainer);
	}

	/**
	 * @see Presenter#go(HasWidgets)
	 */
	public void go(HasWidgets container) {
		mContainer = container;
	}

	/**
	 * Event Bus used to handle all communication between different parts of the
	 * module
	 */
	private final HandlerManager mEventBus;

	/**
	 * The container of this app controller
	 */
	private HasWidgets mContainer;
}
